require('../lib/es6-promise.min.js')

var httphelper={}
 httphelper.get=function(url,data)
{
   var app = getApp()
  if (data==undefined)
  {
    var data = new Object();
  }
  data.time = new Date().getTime(); 
    var p=new Promise(function func(resolve, reject){
 wx.request({
    url: url,
    data:data,
    header: { 'authorization': app == undefined ? '' : app.globalData.token },  
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function(res){
      
      if (res.data && res.data.responseResult != 1)
      {
        wx.showModal({
          title: '错误',
          content: res.data.msg,
          showCancel: false,
          confirmColor: '#ff0000',
          confirmText: '知道了',
          complete: function (r) {
              if (res.data.responseResult==2)
              {
                wx.reLaunch({
                  url: '/pages/login/login'
                })
              }
              return;              
            }          
        })
      }
      //console.log(res);
      resolve(res.data)
     
    },
    fail: function(res) {

      wx.showModal({
        title: '错误',
        content: '连接失败！请检查网络',
        showCancel: false,
        confirmColor: '#ff0000',
        confirmText: '知道了',
      })
      reject(res.data)
    },
    complete: function(res) {
      // complete
      return;
    }
  })

    })
    return p;

}
httphelper.post=function(url,data)
{
  var app = getApp()
  if (data == undefined) {
    var data = new Object();
  }
  data.time = new Date().getTime();
  var p = new Promise(function func(resolve, reject) {
    wx.request({
      url: url,
      data: data,
      header: { 'authorization': app == undefined ? '' : app.globalData.token },  
      method: 'Post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {        
        if (res.data && res.data.responseResult != 1) {
          wx.showModal({
            title: '错误',
            content: res.data.msg,
            showCancel: false,
            confirmColor: '#ff0000',
            confirmText: '知道了',
            complete: function (r) {
              if (res.data.responseResult==2)
              {
                wx.reLaunch({
                  url: '/pages/login/login'
                })
                
              }              
            }
          })
        }
      
        //console.log(res);
        resolve(res.data)

      },
      fail: function (res) {
        wx.showModal({
          title: '错误',
          content: '连接失败！请检查网络',
          showCancel: false,
          confirmColor: '#ff0000',
          confirmText: '知道了',
        })
        reject(res.data)
      },
      complete: function (res) {
        // complete
      
      }
    })

  })
  return p;
}
var param = function (obj) {
  var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
  for (name in obj) {
    value = obj[name];
    if (value instanceof Array) {
      for (i = 0; i < value.length; ++i) {
        subValue = value[i];
        fullSubName = name + '[' + i + ']';
        innerObj = {};
        innerObj[fullSubName] = subValue;
        query += param(innerObj) + '&';
      }
    } else if (value instanceof Object) {
      for (subName in value) {
        subValue = value[subName];
        fullSubName = name + '[' + subName + ']';
        innerObj = {};
        innerObj[fullSubName] = subValue;
        query += param(innerObj) + '&';
      }
    } else if (value !== undefined && value !== null)
      query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
  }
  return query.length ? query.substr(0, query.length - 1) : query;
};
export default httphelper